﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
using PrimaryKeyFunction;

using MySql.Data;
using MySql.Data.MySqlClient;

public partial class Admin_Chr : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillChrCompositionDetails(Convert.ToInt32(id));
                txtChrCompId.Text = string.Empty;
                trtextboxchr.Visible = true;
                trdgChr.Visible = false;
            }


            bindDatagrid();

        }
    }
    private void clearTextboxes()
    {
        txtC.Text = string.Empty;
        txtChrCompId.Text = string.Empty;
        txtH.Text = string.Empty;
        txtN.Text = string.Empty;
        txtS.Text = string.Empty;
        txtAsh.Text = string.Empty;
    }

    private void bindDatagrid()
    {
        string ExpID = (string)Session["ExperimentId"];

        if (ExpID != null)
        {
            if (objService.GetCharComposition(ExpID).Tables[0].Rows.Count > 0)
            {
                dgCHR.DataSource = objService.GetCharComposition(ExpID);
                dgCHR.DataBind();
                trdgChr.Visible = true;
                trtextboxchr.Visible = false;
            }
            else
            {
                trdgChr.Visible = false;
                trtextboxchr.Visible = true;
            }
        }
        else
        {
            trdgChr.Visible = false;
            trtextboxchr.Visible = true;
        }
              
    }
  
    protected void dgChr_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgCHR.PageIndex = e.NewPageIndex;
        bindDatagrid();

    }

    protected void dgCHR_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgCHR.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillChrCompositionDetails(id);
        trdgChr.Visible = false;
        trtextboxchr.Visible = true;
        btnPrevious.Visible = false;
    }

    private void fillChrCompositionDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("chrcomp", id);
        txtChrCompId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtC.Text = ds.Tables[0].Rows[0]["C"].ToString();
        txtH.Text = ds.Tables[0].Rows[0]["H"].ToString();
        txtN.Text = ds.Tables[0].Rows[0]["N"].ToString();
        txtS.Text = ds.Tables[0].Rows[0]["S"].ToString();
        txtAsh.Text = ds.Tables[0].Rows[0]["Ash"].ToString();
    }

    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        if (txtChrCompId.Text != string.Empty)
        {
            trtextboxchr.Visible = true;
            trdgChr.Visible = false;

           
            if (txtC.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "C","null", txtChrCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "C", txtC.Text, txtChrCompId.Text);
            }

            if (txtH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "H", "null", txtChrCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "H", txtH.Text, txtChrCompId.Text);
            }
            if (txtN.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "N", "null", txtChrCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "N", txtN.Text, txtChrCompId.Text);
            }
            if (txtS.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "S", "null", txtChrCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "S", txtS.Text, txtChrCompId.Text);
            }

            if (txtAsh.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "Ash", "null" , txtChrCompId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("chrcomp", "Ash", txtAsh.Text, txtChrCompId.Text);
            }
            
            bindDatagrid();
            clearTextboxes();
        }
        else
        {
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpID = (string)Session["ExperimentId"];
                lblMessage.Text = objService.SaveChrComp(txtC.Text, txtH.Text, txtN.Text, txtS.Text, txtAsh.Text,ExpID);
                clearTextboxes();
                lblExpdetails.Text = "Your Experiment Id is - " + ExpID;
                bindDatagrid();
            }
            else
            {
                string RefeId = (string)Session["ReferenceId"];

                objService.SaveExperiment(null, null, null, null, null, null, null, null, null, null, null, RefeId, null);

                //string ExperimentId = objService.GetLastInsertId().ToString();

                string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);

                lblMessage.Text = objService.SaveChrComp(txtC.Text, txtH.Text, txtN.Text, txtS.Text, txtAsh.Text,ExperimentId);

                clearTextboxes();

                Session["ExperimentId"] = ExperimentId;

                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;

                bindDatagrid();

            }


            string ChrComposistionId = objService.GetLastInsertedId("experiment_id", (string)Session["ExperimentId"], "chrcomp");

            if ((string)Session["YieldId"] != null)
            {
                string YieldId = (string)Session["YieldId"];

                lblMessage.Text = "Chr composition Saved and " + objService.UpdateYield("chrComp_fk", ChrComposistionId, YieldId);

            }
            else
            {
                string ReferenceId = (string)Session["ReferenceId"];

                lblMessage.Text = "Chr composition Saved and " + objService.SaveYield(null, null,null,ChrComposistionId, null, null, null, ReferenceId, (string)Session["ExperimentId"]);

                string YieldId = objService.GetYieldId("chrComp_fk", ChrComposistionId);

                Session["YieldId"] = YieldId;
            }

        }
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreChrComposition.aspx");
    }
}
